import Api from '@api/plugin'
import Mixins from '@mixins'
let Login = {}
Login.install = function (Vue) {
  Vue.use(Api)
  Vue.mixin(Mixins)
  if (document.getElementsByClassName('ui-login').length > 0) {
    return false
  }
  let login = require('./index.vue')
  if (login.default) {
    login = login.default
  }
  let LoginTplObject = Vue.extend(login)
  let $loginVm = new LoginTplObject()
  let LoginTpl = $loginVm.$mount().$el
  document.body.appendChild(LoginTpl)
  Vue.prototype.$login = {
    show (options = {}, _callback) {
      Object.assign($loginVm, options)
      $loginVm.show = true
      $loginVm.$emit = (event, data) => {
        if (event === 'close') {
          this.close()
          if (typeof _callback === 'function') {
            _callback({action: 'close'})
          }
        }
        if (event === 'success') {
          if (typeof _callback === 'function') {
            _callback({action: 'success', data: data, close: this.close})
          }
        }
      }
    },
    close () {
      $loginVm.show = false
    }
  }
}
export default Login
